package com.itheima.reggie.Listenerr;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.exception.BaseContext;
import com.itheima.reggie.exception.R;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class UserCheck implements HandlerInterceptor {
    @Override
    //用户登录拦截
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Long userid =(Long) request.getSession().getAttribute("user");
        Long emid =(Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(userid);
        if(emid==null) {
            if (userid == null) {
                //请求被拦截
                System.out.println("过滤器没2放行");

                response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));


            }
            return false;
        }
       else{
           System.out.println("过滤器2放行了");
           return true;
       }
    }
}
